import java.util.ArrayList;


public class main {

	public static void main(String args[]){


		Selection selection = new Selection();
		// Default output file i.e used if the user doesnt specify any output file explicitly.
		String output="output.csv";
		try{
			
			Long start = System.currentTimeMillis();
			if(args.length!=0)
			{
				String path = args[0];
				//For insertion of records into the heap file from an input csv file.
				if(args.length>1 && args[1].startsWith("-b") )
				{
					boolean create = true;
					//System.out.println("b1");
					ArrayList<Integer> indexes = new ArrayList<Integer>();
					boolean index =true;
					for(int i=1;i<args.length;i++)
					{
						if(args[i].startsWith("-b") && index)
						{
							indexes.add(Integer.parseInt(args[i].substring(2)));
							
						}
						else
						{
							
							CreateIndex c = new CreateIndex();
							c.createIndex(path,indexes);
							//System.out.println("out of create");
							create = false;
							index = false;
							if(args[i].equalsIgnoreCase("-i"))
								{
								//System.out.println("args");
								FileAccess fileAccess = new FileAccess();
								fileAccess.readFile(path);
								}
							break;
						}
					}
					if(create)
					{
						CreateIndex c = new CreateIndex();
							c.createIndex(path,indexes);
					}
				}
				else if(args.length>1 && args[1].equalsIgnoreCase("-i"))
				{
					FileAccess fileAccess = new FileAccess();
					fileAccess.readFile(path);
				}
				//To display the output of the query or directly the contents of the heap file.
				else
				{
					//System.out.println("select");
					selection.select(args);
				}

			}
			else
			{
				throw new Exception("Please enter valid arguments!");			
			}
			
			Long end = System.currentTimeMillis();
			//System.out.println(end-start);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}

}
